﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Collections;
using System.Data;

namespace DAO
{
    public class NhapKhoDAO : DataProvider
    {
        public void NhapKho(NhapKhoDTO nk)
        {
            connect();
            string sql = "UPDATE NguyenVatLieu SET so_luong_ton = so_luong_ton + " + nk.SoLuong + " WHERE nvl_id =" + nk.NvlID;
            executeNonQuery(sql);
            disconnect();
        }

        public int GetDonHangID(string ID)
        {
            connect();
            string sql = "SELECT nvl_id FROM NguyenVatLieu WHERE nvl_id = " + ID + "";
            SqlCommand cmd = new SqlCommand(sql, connection);
            SqlDataReader reader = cmd.ExecuteReader();
            int res = 0;
            while (reader.Read())
                res = reader.GetInt32(0);
            disconnect();
            return res;
        }

        public ArrayList GetDSNguyenVatLieu()
        {
            connect();
            string sql = "select * from NguyenVatLieu";
            adapter = new SqlDataAdapter(sql, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            ArrayList arr = ConvertDataSetToArrayList(dataset);
            disconnect();
            return arr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            NhapKhoDTO nkDTO = new NhapKhoDTO();
            nkDTO.NvlID = int.Parse(dt.Rows[i][0].ToString());
            nkDTO.TenNVL = dt.Rows[i][1].ToString();
            nkDTO.LoaiNVL = bool.Parse(dt.Rows[i][2].ToString());
            nkDTO.DonVi = dt.Rows[i][3].ToString();
            nkDTO.SoLuong = int.Parse(dt.Rows[i][4].ToString());
            
            return (object)nkDTO;
        }
    }
}
